package com.yanze.service.impl;

import com.github.pagehelper.PageHelper;
import com.yanze.mapper.FriendcircleMapper;
import com.yanze.pojo.Friendcircle;
import com.yanze.service.FriendCircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class FriendCircleServiceimpl implements FriendCircleService {

    @Autowired
    FriendcircleMapper friendcircleMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int publishDynamics(Friendcircle friendcircle) {
        int flag = friendcircleMapper.insert(friendcircle);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Friendcircle> getUserDynamics(String userid,int page,int pageSize) {
        //开始分页
        PageHelper.startPage(page, pageSize);
        Example example = new Example(Friendcircle.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmptyOrWhitespace(userid)) {
            criteria.andLike("userid", "%" + userid + "%");
        }
        example.orderBy("time").desc();//按发表时间排序
        List<Friendcircle> dynamics = friendcircleMapper.selectByExample(example);
        return dynamics;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteDynamicById(Long friendid) {
        int flag = friendcircleMapper.deleteByPrimaryKey(friendid);
        return flag;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Friendcircle> getAllDynamics(int page, int pageSize) {
        //开始分页
        PageHelper.startPage(page, pageSize);
        Example example = new Example(Friendcircle.class);
        example.orderBy("time").desc();//按注册时间排序
        List<Friendcircle> userList = friendcircleMapper.selectByExample(example);
        return userList;
    }
}
